var ctx=window.daijia.ctx;


$(document).ready(function(){
	
	$('#price-title').tooltip({
		placement:'top',
	  	html:true,
	  	title:'1、"0"表示不出售该型号油<br/>2、填写加油站实际油价，用户用油的价格=实际油价*平台折扣'
	});
	$('#straightDown-title').tooltip({
	  	placement:'top',
	  	html:true,
	  	title:'显示到油站列表页“降XX”'
	});
	$('#sort-title').tooltip({
	  	placement:'top',
	  	html:true,
	  	title:'小数字排在前面，数字最小的显示在加油站列表页。'
	});
	
	//点击修改价格
	$(document).on("click",".edit-price-button",function(){
		$(this).parent().hide();
		$(this).parent().parent().find(".edit-price").show();
	});
	
	//保存修改价格
	$(document).on("click",".save-price",function(){
		var price_yz=/^(\d+)(.[0-9]{0,2})?$/;
		var editd_price=$(this).parent().parent().find(".edit-price-input").val();
		if(!price_yz.test(editd_price)){
			alert("请输入正确的价格。");
			return false;
		}
		editd_price = parseFloat(editd_price);
		
		if(editd_price >1000){
			alert("价格过大请重新输入。(1000以内)");
			return false;
		}
		
		
		var this_button=$(this);
		this_button.attr("disabled",true);
		$.post(ctx+"/gasstation/saveGasPrice",{"id":this_button.attr("data-id"),"price":editd_price},function(result){
			if(result.success){
				this_button.attr("disabled",false);
				this_button.parent().parent().hide();
				this_button.parent().parent().parent().find(".show-price .show-price-span").html(editd_price);
				this_button.parent().parent().parent().find(".show-price").show();
			}
			else{
				$.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
				this_button.attr("disabled",false);
			}
		});
	});
	
	//点击修改直降
	$(document).on("click",".edit-straightDown-button",function(){
		$(this).parent().hide();
		$(this).parent().parent().find(".edit-straightDown").show();
	});
	
	//保存修改直降
	$(document).on("click",".save-straightDown",function(){
		var editd_straightDown=$(this).parent().parent().find(".edit-straightDown-input").val();
		
		var this_button=$(this);
		this_button.attr("disabled",true);
		$.post(ctx+"/gasstation/saveGasPriceStraightDown",{"id":this_button.attr("data-id"),"straightDown":editd_straightDown},function(result){
			if(result.success){
				this_button.attr("disabled",false);
				this_button.parent().parent().hide();
				this_button.parent().parent().parent().find(".show-straightDown .show-straightDown-span").html(editd_straightDown);
				this_button.parent().parent().parent().find(".show-straightDown").show();
			}
			else{
				$.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
				this_button.attr("disabled",false);
			}
		});
	});
	
	//点击修改直降
	$(document).on("click",".edit-sort-button",function(){
		$(this).parent().hide();
		$(this).parent().parent().find(".edit-sort").show();
	});
	
	//保存修改直降
	$(document).on("click",".save-sort",function(){
		var sort_yz=/^(\d+)$/;
		var editd_sort=$(this).parent().parent().find(".edit-sort-input").val();
		if(!sort_yz.test(editd_sort)){
			alert("请输入正确的序号。");
			return false;
		}
		
		var this_button=$(this);
		this_button.attr("disabled",true);
		$.post(ctx+"/gasstation/saveGasPriceSort",{"id":this_button.attr("data-id"),"sort":editd_sort},function(result){
			if(result.success){
				this_button.attr("disabled",false);
				this_button.parent().parent().hide();
				this_button.parent().parent().parent().find(".show-sort .show-sort-span").html(editd_sort);
				this_button.parent().parent().parent().find(".show-sort").show();
			}
			else{
				$.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
				this_button.attr("disabled",false);
			}
		});
	});
	
	
	
});

